<template>
  <view class="content" @click="authorize">
    <image class="logo" src="/static/logo.png"></image>
    <view>
      <text class="title">{{ title }}</text>
    </view>
    <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">4serwrewrewr</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      title: "Hello",
    };
  },
  onShow() {
    uni.login({
      success: (loginRes) => {
        console.log(loginRes);
        // 获取用户信息
        uni.getUserInfo({
          success: function (infoRes) {
            console.log("用户昵称为：" + infoRes.userInfo.nickName);
          },
        });
      },
      fail: function (ell) {
        console.log(ell);
      },
    });
    // uni.login({
    //   success(res) {
    //     if (res.code) {
    //       //发起网络请求
    //       console.log("临时凭证code：", res.code);
    //       wx.request({
    //         url: "https://api.weixin.qq.com/sns/jscode2session", //仅为示例，并非真实的接口地址
    //         data: {
    //           appid: "wx770fbc519151a100",
    //           secret: "ac85126a317d48950256259adaa1c491",
    //           js_code: res.code,
    //           grant_type: "authorization_code",
    //         },
    //         header: {
    //           "content-type": "application/json", // 默认值
    //         },
    //         success(res) {
    //           console.log("openid：", res.data.openid);
    //           console.log("session_key：", res.data.session_key);
    //           wx.getUserInfo({
    //             success: function (res) {
    //               var userInfo = res.userInfo;
    //               var nickName = userInfo.nickName;
    //               var avatarUrl = userInfo.avatarUrl;
    //               var gender = userInfo.gender; //性别 0：未知、1：男、2：女
    //               var province = userInfo.province;
    //               var city = userInfo.city;
    //               var country = userInfo.country;
    //               console.log(userInfo);
    //             },
    //           });
    //         },
    //       });
    //     } else {
    //       console.log("登录失败！" + res.errMsg);
    //     }
    //   },
    // });
  },
  methods: {
    getPhoneNumber(e) {
      console.log(e.detail.errMsg);
      console.log(e.detail.iv);
      console.log(e.detail.encryptedData);
    },
    authorize() {
      // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
      wx.getSetting({
        success(res) {
          console.log(res);
          // if (!res.authSetting["scope.record"]) {
          //   wx.authorize({
          //     scope: "scope.record",
          //     success() {
          //       // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
          //       wx.startRecord();
          //     },
          //   });
          // }
          if (!res.authSetting["scope.userInfo"]) {
            wx.authorize({
              scope: "scope.userInfo",
              success() {
                wx.chooseAddress({
                  success(res) {
                    console.log("3333333", res);
                  },
                });
              },
            });
          }
        },
      });
    },
  },
};
</script>

<style>
.content {
  text-align: center;
  height: 400upx;
}

.logo {
  height: 200upx;
  width: 200upx;
  margin-top: 200upx;
}

.title {
  font-size: 36upx;
  color: #8f8f94;
}
</style>
